import numpy as np
import matplotlib.pyplot as plt
# import pandas as pd
from scipy.optimize import minimize
from pulp import LpMaximize, LpMinimize, LpProblem, LpVariable, lpSum, value

# 题目参见数模加油站
# 目标函数：求取目标点到每个地点的距离


def fun(x):
    a = np.array([1, 4, 3, 5, 9, 12, 6, 20, 17, 8])
    b = np.array([2, 10, 8, 18, 1, 4, 5, 10, 8, 9])
    f = np.zeros(10)
    for i in range(10):
        f[i] = np.abs(x[0] - a[i]) + np.abs(x[1] - b[i])
    return f


# 真正的目标函数：求取所有距离中的最大值
def objective(x):
    return np.max(fun(x))


# 初始值
x0 = np.array([6, 6])
# 决策变量下界
lb = np.array([3, 4])
# 决策变量上界
ub = np.array([8, 10])

# 约束条件
bounds = [(lb[0], ub[0]), (lb[1], ub[1])]

# 使用SLSQP方法求解
result = minimize(objective, x0, method="SLSQP", bounds=bounds)

x = result.x
feval = fun(x)
print(x)
print(feval)
print(np.max(feval))
